package com.cliff.joinme;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.cliff.joinme.toolkit.MediaStoreHelper;
import com.cliff.joinme.widget.AmazingAdapter;

public class SectionComposerAdapter extends AmazingAdapter {
	
	Object[] sectionNameResArray = {R.string.section_localmedia, R.string.section_network};	
	ComposerListLine sectionLocalMediaArray[] = new ComposerListLine[]{
		new ComposerListLine(R.drawable.folder_images, R.string.section_localmedia_images, R.string.section_localmedia_images_desc),	
		new ComposerListLine(R.drawable.folder_music, R.string.section_localmedia_music, R.string.section_localmedia_music_desc),	
		new ComposerListLine(R.drawable.folder_videos, R.string.section_localmedia_videos, R.string.section_localmedia_videos_desc)	
	};
	
	List<ComposerListLine> sectionHomeNetworkList = new ArrayList<ComposerListLine>();
	
	Activity mActivity = null;
	
	public SectionComposerAdapter(Activity activity) {
		mActivity = activity;
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return sectionLocalMediaArray.length + sectionHomeNetworkList.size();
	}

	@Override
	public Object getItem(int arg0) {
		// TODO Auto-generated method stub
		if (arg0 < sectionLocalMediaArray.length)
			return sectionLocalMediaArray[arg0];

		return null;
	}

	@Override
	public long getItemId(int arg0) {
		// TODO Auto-generated method stub
		return arg0;
	}

	@Override
	protected void onNextPageRequested(int page) {
		// TODO Auto-generated method stub
		
	}

	@Override
	protected void bindSectionHeader(View view, int position,
			boolean displaySectionHeader) {
		// TODO Auto-generated method stub
		if (displaySectionHeader) {
			view.findViewById(R.id.header).setVisibility(View.VISIBLE);
			TextView lSectionTitle = (TextView) view.findViewById(R.id.header);
			lSectionTitle.setText((Integer)sectionNameResArray[getSectionForPosition(position)]);
		} else {
			view.findViewById(R.id.header).setVisibility(View.GONE);
		}
	}

	@Override
	public View getAmazingView(int position, View convertView,
			ViewGroup parent) {
		View res = convertView;
		if (res == null) res = mActivity.getLayoutInflater().inflate(R.layout.item_composer, null);
		
		ImageView lIcon = (ImageView) res.findViewById(R.id.lineicon);
		TextView lName = (TextView) res.findViewById(R.id.linename);
		TextView lDesc = (TextView) res.findViewById(R.id.linedesc);
		
		if (position < sectionLocalMediaArray.length) {
			ComposerListLine line = sectionLocalMediaArray[position];
			lIcon.setImageResource(line.resIdIcon);
			lName.setText(line.resIdName);
			MediaStoreHelper helper = MediaStoreHelper.getInstance();
			String desc = mActivity.getResources().getString(line.resIdDesc);
			switch (line.resIdDesc) {
			case R.string.section_localmedia_images_desc: 
				lDesc.setText(String.format(desc, helper.getImageCount(mActivity)));
				break;
			case R.string.section_localmedia_music_desc: 
				lDesc.setText(String.format(desc, helper.getMusicCount(mActivity)));
				break;
			case R.string.section_localmedia_videos_desc: 
				lDesc.setText(String.format(desc, helper.getVideoCount(mActivity)));
				break;
			}
		}		
		return res;
	}

	@Override
	public void configurePinnedHeader(View header, int position, int alpha) {
		TextView lSectionHeader = (TextView)header;
		lSectionHeader.setText((Integer)getSections()[getSectionForPosition(position)]);
//		lSectionHeader.setBackgroundColor(alpha << 24 | (0xbbffbb));
//		lSectionHeader.setTextColor(alpha << 24 | (0x000000));
	}

	@Override
	public int getPositionForSection(int section) {
		// TODO Auto-generated method stub
		switch (section) {
		case 0:
			return 0;
		case 1:
			return sectionLocalMediaArray.length;
		}
		return 0;
	}

	@Override
	public int getSectionForPosition(int position) {
		// TODO Auto-generated method stub
		if (position >= sectionLocalMediaArray.length )
			return 1;
		return 0;
	}

	@Override
	public Object[] getSections() {
		// TODO Auto-generated method stub
		return sectionNameResArray;
	}
	
	public class ComposerListLine {
		public int resIdIcon = 0;
		public int resIdName = 0;
		public int resIdDesc = 0;
		
		public ComposerListLine(int icon, int name, int desc) {
			resIdIcon = icon;
			resIdName = name;
			resIdDesc = desc;
		}
	}
	
}
